<?php //include_once VIEWPATH.'common/dialog.php'; ?>
<script type="text/javascript">
    function on_submit(){
        var path = $("input[name='path[]']").val();
        console.log(path);
        if(!path){
            alert("请上传图片");
            return false;
        }
        $.ajax({
            type:'post',
            url:"index.php?c=imgLib&m=add_img",
            data:$('#form_img').serialize(),
            dataType:'json',
            success:function(result){
                if(result['status'] == 1){
                    alert("图片更新成功");
                    var img_src=$('.img-thumbnail').attr('src');
                    $('#img<?php echo $type != 1?'_u':''?>_<?php echo $related_id;?>').attr('src',img_src);
                    $('#close_<?php echo $related_id;?>').show();

                    <?php
                        if($type!=1){
                    ?>
                    $('#avator_id').val(result['id']);
                    <?php
                        }
                    ?>
                    $('#add_img').modal('hide');
                    $('#add_img').click();
                    // $('#add_img').remove()

                    // $('.close').trigger("click");
                }else{
                    alert("删除更新失败")
                }
            }
        });
        // $('#form_img').submit();
    }
    $("#add_img").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
</script>
<style>
    .radio-inline input[type=radio]{margin-top:0px;}
</style>
<div class="tab-pane active" id="horizontal-form">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h2 class="modal-title"><?php echo $pageTitle;?></h2>
    </div>
    <form class="form-horizontal" name="form_add_img" id="form_img" action="index.php?c=<?php echo $_REQUEST['c'];?>&m=<?php echo $_REQUEST['m'];?>" method="post" enctype="multipart/form-data">
        <input type="hidden" name="submits" value="1"/>
        <input type="hidden" name="id" value="<?php echo $related_id;?>"/>
        <input type="hidden" name="type" value="<?php echo $type;?>"/>

        <div class="form-group">
            <label for="icon" class="col-sm-2 control-label">图标</label>
            <div class="col-sm-8">
                <p><a href="javascript:void(0)" class="upload_img"><input type="file" id="upload_img">上传图片</a></p>
                <ul id="upload_list" class="list-inline">

                </ul>
            </div>
        </div>

        <div class="modal-footer">
            <button type="reset" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" onclick="on_submit()" class="btn btn-primary">保存</button>
        </div>

    </form>
</div>
<script id="upload-tpl" type="text/html">
    <li>
        <label>
        <img src="{{src}}" class="img-thumbnail" width="160px" height="90px" />
        <input type="hidden" name="path[]" value={{name}} />
        <button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<!--        <input type="radio" value="{{name}}" name="is_cover">选为封面图-->
        </label>
    </li>
</script>

<script type="text/javascript">
    $(function () {
        var c_client = new OSS.Wrapper({
            region: 'oss-cn-shanghai',
            accessKeyId: 'LTAIqUbOPUz16zaX',
            accessKeySecret: 'FzQDTPRfbKRrJ3U9d6psiIa36iJ3lF',
            bucket: 'nuanguang-img'
        });
        $('#upload_img').bind('change', function (e) {
            var file = e.target.files[0];
            console.log(file);
            var ext=/.[^.]+$/.exec(file.name);
            var storeAs = 'img_lib/a'+new Date()*1 + parseInt(Math.random()*100)+ext;
            c_client.multipartUpload(storeAs, file).then(function (result) {
                console.log(result)
                var html = template('upload-tpl', {src:"http://nuanguang-img.oss-cn-shanghai.aliyuncs.com/"+storeAs,name:storeAs});
                $("#upload_list").append(html);
            }).catch(function (err) {
                console.log(err);
            });
        });
        $('#upload_list').on('click','.close',function () {
            $(this).closest('li').remove();
        });
    })

</script>